const cloud = require('wx-server-sdk');
cloud.init();

exports.main = async (event, context) => {
  const db = cloud.database();
  const _ = db.command;
  const { openid, stockCode } = event;
  console.log(stockCode)
  console.log(openid)
  const selectedStock = await db.collection('selectedStock').where({ openid }).get();

  if (selectedStock.data.length === 0) {
    return await db.collection('selectedStock').add({
      data: {
        openid,
        stockCodes: [stockCode]
      }
    });
  } else {
    return await db.collection('selectedStock').where({ openid }).update({
      data: {
        stockCodes: _.addToSet(stockCode)
      }
    });
  }
};
